﻿using Furion;
using Furion.FriendlyException;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace BaseNet.Core.Utils
{
    /// <summary>
    /// sqlsugar扩展方法
    /// </summary>
    public static class SugarExt
    {
        public static void ExtInsertable<T>(this SqlSugarClient Db, T entity, string ErrorText = null) where T : class, new()
        {
            if (Db.Insertable(entity).ExecuteCommand() != 1)
                throw Oops.Oh(ErrorText ?? "添加至数据库失败");
        }

        public static void ExtInsertable<T>(this SqlSugarClient Db, List<T> list, string ErrorText = null) where T : class, new()
        {
            if (list.Count > 0)
            {
                if (Db.Insertable(list).ExecuteCommand() != list.Count)
                    throw Oops.Oh(ErrorText ?? "添加至数据库失败");
            }
        }

        public static void ExtUpdateable<T>(this SqlSugarClient Db, T entity, string ErrorText = null) where T : class, new()
        {
            foreach (PropertyInfo p in entity.GetType().GetProperties())
            {
                if (p.Name == "LastModificationTime")
                {
                    p.SetValue(entity, DateTimeOffset.Now);
                }
                else if (p.Name == "LastModifierUserId")
                {
                    p.SetValue(entity, App.User?.FindFirstValue("UserId"));
                }
                else if (p.Name == "LastModifierUserName")
                {
                    p.SetValue(entity, App.User?.FindFirstValue("Account"));
                }
            }
            if (Db.Updateable(entity).ExecuteCommand() != 1)
                throw Oops.Oh(ErrorText ?? "更新至数据库失败");
        }

        public static void ExtUpdateable<T>(this SqlSugarClient Db, List<T> list, string ErrorText = null) where T : class, new()
        {
            if (list.Count > 0)
            {
                foreach (var entity in list)
                {
                    foreach (PropertyInfo p in entity.GetType().GetProperties())
                    {
                        if (p.Name == "LastModificationTime")
                        {
                            p.SetValue(entity, DateTimeOffset.Now);
                        }
                        else if (p.Name == "LastModifierUserId")
                        {
                            p.SetValue(entity, App.User?.FindFirstValue("UserId"));
                        }
                        else if (p.Name == "LastModifierUserName")
                        {
                            p.SetValue(entity, App.User?.FindFirstValue("Account"));
                        }
                    }
                }
                if (Db.Updateable(list).ExecuteCommand() != list.Count)
                    throw Oops.Oh(ErrorText ?? "更新至数据库失败");
            }
        }

        public static void ExtDeleteable<T>(this SqlSugarClient Db, T entity, string ErrorText = null) where T : class, new()
        {
            foreach (PropertyInfo p in entity.GetType().GetProperties())
            {
                if (p.Name == "IsDeleted")
                {
                    p.SetValue(entity, true);
                }
                else if (p.Name == "DeleterUserId")
                {
                    p.SetValue(entity, App.User?.FindFirstValue("UserId"));
                }
                else if (p.Name == "DeleterUserName")
                {
                    p.SetValue(entity, App.User?.FindFirstValue("Account"));
                }
                else if (p.Name == "DeletionTime")
                {
                    p.SetValue(entity, DateTimeOffset.Now);
                }
            }
            if (Db.Deleteable(entity).ExecuteCommand() != 1)
                throw Oops.Oh(ErrorText ?? "从数据库删除失败");
        }

        public static void ExtDeleteable<T>(this SqlSugarClient Db, List<T> list, string ErrorText = null) where T : class, new()
        {
            if (list.Count > 0)
            {
                foreach (var entity in list)
                {
                    foreach (PropertyInfo p in entity.GetType().GetProperties())
                    {
                        if (p.Name == "IsDeleted")
                        {
                            p.SetValue(entity, true);
                        }
                        else if (p.Name == "DeleterUserId")
                        {
                            p.SetValue(entity, App.User?.FindFirstValue("UserId"));
                        }
                        else if (p.Name == "DeleterUserName")
                        {
                            p.SetValue(entity, App.User?.FindFirstValue("Account"));
                        }
                        else if (p.Name == "DeletionTime")
                        {
                            p.SetValue(entity, DateTimeOffset.Now);
                        }
                    }
                }
                if (Db.Deleteable(list).ExecuteCommand() != list.Count)
                    throw Oops.Oh(ErrorText ?? "从数据库删除失败");
            }
        }






        public static async Task ExtInsertableAsync<T>(this SqlSugarClient Db, T entity, string ErrorText = null) where T : class, new()
        {
            if ((await Db.Insertable(entity).ExecuteCommandAsync()) != 1)
                throw Oops.Oh(ErrorText ?? "添加至数据库失败");
        }

        public static async Task ExtInsertableAsync<T>(this SqlSugarClient Db, List<T> list, string ErrorText = null) where T : class, new()
        {
            if (list.Count > 0)
            {
                if ((await Db.Insertable(list).ExecuteCommandAsync()) != list.Count)
                    throw Oops.Oh(ErrorText ?? "添加至数据库失败");
            }
        }

        public static async Task ExtUpdateableAsync<T>(this SqlSugarClient Db, T entity, string ErrorText = null) where T : class, new()
        {
            foreach (PropertyInfo p in entity.GetType().GetProperties())
            {
                if (p.Name == "LastModificationTime")
                {
                    p.SetValue(entity, DateTimeOffset.Now);
                }
                else if (p.Name == "LastModifierUserId")
                {
                    p.SetValue(entity, App.User?.FindFirstValue("UserId"));
                }
                else if (p.Name == "LastModifierUserName")
                {
                    p.SetValue(entity, App.User?.FindFirstValue("Account"));
                }
            }
            if ((await Db.Updateable(entity).ExecuteCommandAsync()) != 1)
                throw Oops.Oh(ErrorText ?? "更新至数据库失败");
        }

        public static async Task ExtUpdateableAsync<T>(this SqlSugarClient Db, List<T> list, string ErrorText = null) where T : class, new()
        {
            if (list.Count > 0)
            {
                foreach (var entity in list)
                {
                    foreach (PropertyInfo p in entity.GetType().GetProperties())
                    {
                        if (p.Name == "LastModificationTime")
                        {
                            p.SetValue(entity, DateTimeOffset.Now);
                        }
                        else if (p.Name == "LastModifierUserId")
                        {
                            p.SetValue(entity, App.User?.FindFirstValue("UserId"));
                        }
                        else if (p.Name == "LastModifierUserName")
                        {
                            p.SetValue(entity, App.User?.FindFirstValue("Account"));
                        }
                    }
                }
                if ((await Db.Updateable(list).ExecuteCommandAsync()) != list.Count)
                    throw Oops.Oh(ErrorText ?? "更新至数据库失败");
            }
        }

        public static async Task ExtDeleteableAsync<T>(this SqlSugarClient Db, T entity, string ErrorText = null) where T : class, new()
        {
            foreach (PropertyInfo p in entity.GetType().GetProperties())
            {
                if (p.Name == "IsDeleted")
                {
                    p.SetValue(entity, true);
                }
                else if (p.Name == "DeleterUserId")
                {
                    p.SetValue(entity, App.User?.FindFirstValue("UserId"));
                }
                else if (p.Name == "DeleterUserName")
                {
                    p.SetValue(entity, App.User?.FindFirstValue("Account"));
                }
                else if (p.Name == "DeletionTime")
                {
                    p.SetValue(entity, DateTimeOffset.Now);
                }
            }
            if ((await Db.Updateable(entity).ExecuteCommandAsync()) != 1)
                throw Oops.Oh(ErrorText ?? "从数据库删除失败");
        }

        public static async Task ExtDeleteableAsync<T>(this SqlSugarClient Db, List<T> list, string ErrorText = null) where T : class, new()
        {
            if (list.Count > 0)
            {
                foreach (var entity in list)
                {
                    foreach (PropertyInfo p in entity.GetType().GetProperties())
                    {
                        if (p.Name == "IsDeleted")
                        {
                            p.SetValue(entity, true);
                        }
                        else if (p.Name == "DeleterUserId")
                        {
                            p.SetValue(entity, App.User?.FindFirstValue("UserId"));
                        }
                        else if (p.Name == "DeleterUserName")
                        {
                            p.SetValue(entity, App.User?.FindFirstValue("Account"));
                        }
                        else if (p.Name == "DeletionTime")
                        {
                            p.SetValue(entity, DateTimeOffset.Now);
                        }
                    }
                }
                if ((await Db.Updateable(list).ExecuteCommandAsync()) != list.Count)
                    throw Oops.Oh(ErrorText ?? "从数据库删除失败");
            }
        }
    }
}
